import api from '../../api';
import { message } from 'antd';
import accounting from 'accounting';

export default {
  namespace: 'bondInformationM',

  state: {
    bondTypeList: '',
    bondTypeListPBC: [],
  },

  effects: {
    // 获取虚拟标的证券代码code name
    *getStockInfo({ payload, callback }, { call, put }) {
      try {
        const res = yield call(api.bondInformation.getStock, payload);
        callback(res);
      } catch (e) {
        console.log(e);
      }
    },
    // 还有不是当天虚拟标的的查询 点击试算按钮的接口  /portfolioTrial/virtualSubjectTrial?id=
    *virtualSubjectTrial({ payload, callback }, { call, put }) {
      try {
        const res = yield call(api.bondInformation.virtualSubjectTrial, payload);
        callback(res);
      } catch (e) {
        console.log(e);
      }
    },
    // 保存虚拟标的
    *saveVirtualSubject({ payload, callback }, { call, put }) {
      try {
        const res = yield call(api.bondInformation.saveVirtualSubject, payload);
        callback(res);
      } catch (e) {
        console.log(e);
      }
    },

    //查询债券发行信息数据
    *queryTableList({ payload, callback }, { call, put }) {
      const response = yield call(api.bondInformation.queryBondInformation, payload);
      if (response && response.code == 0) {
        response.data?.dataSource.forEach((item) => {
          item.tradingCode = item.tradingCode + ' - ' + item.secuabbr;
        });
        response.data?.columns.forEach((value) => {
          if (value.key == 'prepaindAmount' || value.key == 'issueSize') {
            value['render'] = (val) => {
              if (val) {
                // return accounting.formatNumber((Number(val) / 10000).toFixed(2)
                return accounting.formatNumber(Number(val) / 10000, 2);
              } else {
                return '--';
              }
            };
          }
          if (value.key == 'prepaindAmount' || value.key == 'issueSize') {
            value['type'] = 'money';
          }
          if (value.key == 'couponRate' || value.key == 'prepaindProp' || value.key == 'bondMaturity') {
            value['type'] = 'number';
          }
          if (value.key == 'secucategoryCodeiiName') {
            value['width'] = '120px';
          }
          if (value.key == 'tradingCode') {
            value['width'] = '200px';
          }
        });
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(`查询列表失败，失败原因：${data?.msg}`);
        console.error(`查询列表失败，失败原因：${data?.msg}`, '接口名称： findAll 接口：instrument-server/bondissuerinfo/findAll');
      }
    },

    //删除接口
    *deleteAccountById({ payload, callback }, { call, put }) {
      const response = yield call(api.bondInformation.deleteAccountById, payload);
      if (response && response.code === 0) {
        callback('success');
        message.success('删除成功');
      } else {
        message.error(`删除失败，失败原因：${response?.msg}`);
        console.error(`删除失败，失败原因：${response?.msg}`, '接口名称： deleteById 接口：instrument-server/bondissuerinfo/deleteById');
      }
    },

    // 获取发行人下拉列表
    *getIssuerList({ payload, callback }, { call, put }) {
      const response = yield call(api.getIssuerList, payload);
      if (response && response.code == 0) {
        callback(response.data.dataSource);
      } else {
        message.error(`获取发行人数据失败，失败原因：${response?.msg}`);
        console.error(`获取发行人数据失败，失败原因：${response?.msg}`, '接口名称： issuerInfo 接口：instrument-server/bondissuerinfo/issuerInfo');
      }
    },
    // 新增债券信息
    *addSecucategory({ payload, callback }, { call, put }) {
      const response = yield call(api.bondInformation.addSecucategory, payload);
      if (response && response.code === 0) {
        callback(response);
        message.success('新增债券信息成功');
      } else {
        message.error(`新增债券信息失败，失败原因：${response?.msg}`);
        console.error(`新增债券信息失败，失败原因：${response?.msg}`, '接口名称： saveBond 接口：instrument-server/bondissuerinfo/saveBond');
      }
    },

    // 修改债券信息
    *editSecucategoryById({ payload, callback }, { call, put }) {
      const response = yield call(api.bondInformation.editSecucategoryById, payload);
      if (response && response.code === 0) {
        callback(response);
        message.success('修改债券信息成功');
      } else {
        message.error(`修改债券信息失败，失败原因：${response?.msg}`);
        console.error(`修改债券信息失败，失败原因：${response?.msg}`, '接口名称： updateBond 接口：instrument-server/bondissuerinfo/updateBond');
      }
    },
    // 上市转换
    *changeSecucode({ payload, callback }, { call, put }) {
      const res = yield call(api.bondInformation.changeSecucode, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success(res.message || '上市转换成功');
      } else {
        callback('fail');
        message.error(`上市转换失败，失败原因：${res?.message}`);
        console.error(`上市转换失败，失败原因：${res?.message}`, '接口名称： changeSecucode 接口：bondTrade/changeSecucode');
      }
    },
    // 获取基本信息
    *queryBondBasicInfoDetail({ payload, callback }, { call, put, select }) {
      const response = yield call(api.bondInformation.queryBondBasicInfoDetail, payload);
      if (response && response.code == 0) {
        let tempObj = {};
        const res = response.data;
        for (let k in res) {
          if (k == 'couponRate') {
            //利率 4.400000%   4.40
            tempObj[k] = res[k] ? parseFloat(res[k].substr(0, res[k].length - 1)).toFixed(2) : undefined;
          }
          if (k == 'issueSize') {
            //发行总量 10.0000亿 10.0000*10000
            tempObj[k] = res[k] ? (parseFloat(res[k].substr(0, res[k].length - 1)) * 10000).toFixed(4) : undefined;
          }
          if (k == 'categoryNamei') {
            // 债券类型 一般企业债
            tempObj[k] = res[k] ? res[k] : undefined;
          }
        }
        callback(tempObj || {});
      } else {
        callback();
        message.error(`获取债券基础信息失败，失败原因：${res?.msg}`);
        console.error(`获取债券基础信息失败，失败原因：${res?.msg}`, '接口名称： 查询的证券代码 接口：bondInfo/bondBasicInfoDetail/');
      }
    },
    // 获取债券发行信息
    *queryBondIssuerInfoDetail({ payload, callback }, { call, put, select }) {
      const response = yield call(api.bondBaseInfo.queryBondIssuerInfoDetail, payload);
      if (response && response.code == 0) {
        let tempObj = {};
        const res = response.data;
        for (let k in res) {
          if (k == 'issuer') {
            // 发行人 鲁能集团有限公司
            tempObj[k] = res[k] ? res[k] : undefined;
          }
        }
        callback(tempObj || {});
      } else {
        callback();
        message.error(`查询债券发行信息失败，失败原因：${res?.msg}`);
        console.error(`查询债券发行信息失败，失败原因：${res?.msg}`, '接口名称： bondIssuerInfoDetail 接口：instrument-server/bond/bondIssuerInfoDetail');
      }
    },
    // 获取持仓日期下拉
    *queryHoldDataList({ payload, callback }, { call, put }) {
      const res = yield call(api.bondBaseInfo.queryHoldDataList, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.msg || '获取持仓日期下拉列表失败！');
      }
    },

    reducers: {
      save(state, { payload }) {
        return {
          ...state,
          ...payload,
        };
      },
    },
  },
};
